// 地图显示图标函数
let randomPos = []
    // 声明变量保存搜索框内容
let searchVal = ""
const randomFunc = function(posArr) {
    console.log()
    let arr = []
    for (var i = 0; i < 100; i++) {

        arr.push([posArr[0] + parseFloat(Math.random().toFixed(6)), posArr[1] - parseFloat(Math.random().toFixed(6))])
    }
    return arr
}
const mapShowCoverage = function(map, houseList) {
    if (!houseList || !houseList.length || !map) {
        return
    }
    var LabelsData = []
        // 图标
    var icon = {
        // 图标类型，现阶段只支持 image 类型
        type: 'image',
        // 图片 url
        image: './img/dingwei.png',
        // 图片尺寸
        size: [32, 32],
        // 图片相对 position 的锚点，默认为 bottom-center
        anchor: 'center',
    };
    // 显示的字体样式
    var textStyle = {
        fontSize: 12,
        fontWeight: 'bold',
        fillColor: '#FB8732',
        strokeColor: '#fff',
        strokeWidth: 2,
        fold: true,
        padding: '2, 5',
    };
    houseList.forEach((house, hidx) => {
        if (!LabelsData[hidx]) {
            LabelsData[hidx] = {}
        }
        LabelsData[hidx].position = [house[0], house[1]]
        LabelsData[hidx].icon = icon
        LabelsData[hidx].opacity = 1
        LabelsData[hidx].hIndex = house.hIndex
        LabelsData[hidx].rRhIndex = house.rRhIndex
        LabelsData[hidx].text = {}
        LabelsData[hidx].text.content = `${house.Name}\n${parseFloat(house.hAvgeragePrice / 10000).toFixed(1)}万`
        LabelsData[hidx].text.direction = "right"
        LabelsData[hidx].text.style = textStyle
    })
    var markers = [];
    var allowCollision = false;
    var layer = new AMap.LabelsLayer({
        zooms: [3, 20],
        zIndex: 1000,
        // collision: false,
        // 设置 allowCollision：true，可以让标注避让用户的标注
        allowCollision,
    });
    layer.add(markers);
    // 图层添加到地图
    map.add(layer);

    // 初始化 labelMarker
    for (var i = 0; i < LabelsData.length; i++) {
        var curData = LabelsData[i];
        curData.extData = {
            index: i
        };

        var labelMarker = new AMap.LabelMarker(curData);

        markers.push(labelMarker);

    }
    // 将 marker 添加到图层
    layer.add(markers);
    markers.forEach(item => {
        item.on("click", showHouseInfo)
    });

    function showHouseInfo(e) {

        var _this = e.target
        console.log(_this)
        console.log(map.getAllOverlays())
        return
        mapHIndex = _this.Ce.hIndex
        mapRHIndex = _this.Ce.rRhIndex
        var housingDetail = document.querySelector(".hSecListBox")
        var uSHousingList_js = document.querySelector(".uRentingHousingList-js")
        if (uSHousingList_js) {
            uSHousingList_js.remove()
        }
        if (!housingDetail) {
            appendScript("uRentingHousingList-js", "/js/user/public/components/rentHouse/uRentHousingList.js")
        } else {
            housingDetail.remove()
            appendScript("uRentingHousingList-js", "/js/user/public/components/rentHouse/uRentHousingList.js")
        }
    }

};
//初始化地图

// closeIconBtn.onclick = function(e) {
//     map.clearMap();
// }

//jshint -W098
function cityPickerReady(cityPicker) {

    window.cityPicker = cityPicker;

    //引用domLibaray(Zepto)
    AMapUI.load(['lib/$'], function($) {

        //选中某个城市
        cityPicker.on('citySelected', function(cityInfo) {
            console.log(cityInfo)
            let cityIcons = randomFunc([cityInfo.lng, cityInfo.lat])
            console.log(cityIcons)
            mapShowCoverage(map, cityIcons)
                //隐藏城市列表面板
            cityPicker.hideImmediately();

            //清除搜索框内容
            cityPicker.clearSearch();


            notifyCityInfo(cityInfo);
            console.log(map.getBounds()) // 屏幕范围
        });

        $('.changeCity').click(function() {
            cityPicker.toggleShowHide();
        });

        var marker = new AMap.Marker();

        /**
         * 显示对应的城市信息
         */
        //jshint -W003
        function notifyCityInfo(cityInfo) {

            //$("#selectedCityInfo").html(JSON.stringify(cityInfo, null, 2));

            marker.setMap(map);

            marker.setPosition([cityInfo.lng, cityInfo.lat]);

            map.setCenter(marker.getPosition(), true);
        }

    });
}
// 监听输入事件
const inputSearch = (inputDom) => {
    var doing = false;
    //
    // 获取输入框元素并添加监听事件
    let searchInput = document.querySelector(inputDom)
        // 搜索事件
    searchInput.addEventListener('compositionstart', function(e) {
        doing = true;
    }, false);
    searchInput.addEventListener('input', function(e) {
        if (!doing) {
            searchVal = e.target.value //  保存搜索内容
            getSearchVal(e.target);
        }
    }, false);
    searchInput.addEventListener('compositionend', function(e) {
        doing = false;
        searchVal = e.target.value //  保存搜索内容
        getSearchVal(e.target);
    })
}
const getSearchVal = function(e) {
    console.log(e.value.trim())
    console.log(document.querySelector(".amap-ui-poi-picker-sugg-container > ul"))
}